//-------------------------------------------------------------------------------//

#include "globals.h"
#include "game.h"

//-------------------------------------------------------------------------------//

Game::Game()
{}

//-------------------------------------------------------------------------------//

Game::~Game()
{}

//-------------------------------------------------------------------------------//
// Game::Initiate() - This sets up an HGE window based on data loaded from a file.
//-------------------------------------------------------------------------------//
void Game::Initiate ( string filename )
{
    m_HGE = hgeCreate ( HGE_VERSION );

    // check if the filename contains anything
    if ( filename.empty() == false )
    {
        TiXmlDocument doc;

        // check if we successully loaded the document
        if ( doc.LoadFile ( filename ) == true )
        {
            // iteratore through every child of the "Window" node
            for ( TiXmlElement* element = doc.FirstChildElement ( "Window" )->FirstChildElement(); element; element = element->NextSiblingElement() )
            {
                // If the name of the element is "Caption", get the text from and use it to set HGE_TITLE
                if ( element->ValueStr().compare ( "Caption" ) == 0 )
                    m_HGE->System_SetState ( HGE_TITLE, element->GetText() );
                // If the name of the element is "Files", get the log and resource filename from the attributes
                else if ( element->ValueStr().compare ( "Files" ) == 0 )
                {
                    m_HGE->System_SetState ( HGE_LOGFILE, element->Attribute ( "log" ) );
                    m_rManager = new hgeResourceManager ( element->Attribute ( "resources" ) );
                }
                // If the name of the element is "Options", save some HGE bool states that are loaded from file
                else if ( element->ValueStr().compare ( "Options" ) == 0 )
                {
                    if ( string ( element->Attribute ( "windowed" ) ).compare ( "true" ) == 0 )
                        m_HGE->System_SetState ( HGE_WINDOWED, true );
                    else
                        m_HGE->System_SetState ( HGE_WINDOWED, false );

                    if ( string ( element->Attribute ( "showSplash" ) ).compare ( "true" ) == 0 )
                        m_HGE->System_SetState ( HGE_SHOWSPLASH, true );
                    else
                        m_HGE->System_SetState ( HGE_SHOWSPLASH, false );

                    if ( string ( element->Attribute ( "useSound" ) ).compare ( "true" ) == 0 )
                        m_HGE->System_SetState ( HGE_USESOUND, true );
                    else
                        m_HGE->System_SetState ( HGE_USESOUND, false );
                }
                // If the name of the element is "Size", grab the window width and height attributes, then convert them to int
                else if ( element->ValueStr().compare ( "Size" ) == 0 )
                {
                    m_HGE->System_SetState ( HGE_SCREENWIDTH, atoi ( element->Attribute ( "width" ) ) );
                    m_HGE->System_SetState ( HGE_SCREENHEIGHT, atoi ( element->Attribute ( "height" ) ) );
                }
            }
        }
        else
            // doc.LoadFile() failed to load, not many reasons to check why
            m_HGE->System_Log ( "Failed to load the %s document.", filename.c_str() );
    }
    else
        // filename string was empty
        m_HGE->System_Log ( "%s is an invalid filename.", filename.c_str() );
}

//-------------------------------------------------------------------------------//
// Game::Destroy() - Deletes HGE objects, and resource managers.
//-------------------------------------------------------------------------------//
void Game::Destroy()
{
    SAFE_DELETE ( m_rManager );
    m_HGE->Release();
}

//-------------------------------------------------------------------------------//
